package com.allsheng.widget;

import com.example.allshengmedical.R;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

/**
 *	便捷的Dialog
 * @author Administrator
 *
 */

public class AllShengDialog extends Dialog {
	private Context context;
	private String title;
	private String message;
	private String rightText, leftText;
	private View view;
	private ClickListenerInterface clickListenerInterface;
	private TextView tvLeft;

	/**
	 * @param title显示标题
	 * @param message显示内容
	 * @param rightText确定
	 */
	public AllShengDialog(Context context, String title, String message,
			String rightText) {
		super(context, R.style.dialog);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.title = title;
		this.message = message;
		this.rightText = rightText;

	}

	public AllShengDialog(Context context, String title, String message,
			String rightText, String leftText) {
		super(context, R.style.dialog);
		// TODO Auto-generated constructor stub
		this.context = context;
		this.title = title;
		this.message = message;
		this.rightText = rightText;
		this.leftText = leftText;

	}

	public interface ClickListenerInterface {
		public void Cancle();

		public void OK();

	}

	public void setClicklistener(ClickListenerInterface clickListenerInterface) {
		this.clickListenerInterface = clickListenerInterface;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		LayoutInflater inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.dialog_my, null);
		setContentView(view);
		setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
		TextView tvMessage = (TextView) view.findViewById(R.id.tvMessage);
		tvLeft = (TextView) view.findViewById(R.id.tvBtnLeft);

		TextView tvRight = (TextView) view.findViewById(R.id.tvBtnRight);
		TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
		view = view.findViewById(R.id.view);

		if ("".equals(title)) {
			tvTitle.setVisibility(View.GONE);
		} else {
			tvTitle.setText(title);
		}

		if (TextUtils.isEmpty(leftText)) {
			tvLeft.setVisibility(View.GONE);
			tvLeft.setVisibility(View.GONE);
		} else {
			tvLeft.setText(leftText);
			tvLeft.setVisibility(View.VISIBLE);
		}
		tvMessage.setText(message);
		tvLeft.setText(leftText);
		tvRight.setText(rightText);
		tvLeft.setOnClickListener(new clickListener());
		tvRight.setOnClickListener(new clickListener());

		Window dialogWindow = getWindow();
		WindowManager.LayoutParams lp = dialogWindow.getAttributes();
		DisplayMetrics d = context.getResources().getDisplayMetrics();
		lp.width = (int) (d.widthPixels * 0.6);
		dialogWindow.setAttributes(lp);
	}

	private class clickListener implements View.OnClickListener {

		public void onClick(View v) {

			int id = v.getId();
			switch (id) {
			case R.id.tvBtnLeft:
				clickListenerInterface.Cancle();
				break;
			case R.id.tvBtnRight:
				clickListenerInterface.OK();
				break;

			default:
				break;
			}
		}
	};
}
